/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Aug 27, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.colorpalette.v1;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;

public class ColorPalette extends JPanel{

    private String[] nameList;
    public ColorPalette() {
         initComponents();
        postInit();
    }
    public ColorPalette(String[] nameList) {
        this.nameList =nameList;
        initComponents();
        postInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

       listScrollPane = new javax.swing.JScrollPane();
        targetList = new javax.swing.JList();
        maintSwatch = new MainSwatchPanel ();
        recentSwatch = new RecentSwatchPanel();
        selectedPane = new SelectPanel();
        selectL = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        listScrollPane.setPreferredSize(new java.awt.Dimension(73, 80));
        DefaultListModel model=new DefaultListModel();
        if(nameList==null||nameList.length <=0)
            nameList =new String[] {"item1","item2"};//{ "background", "selection", "axis" };  // {"item1","item2"};//
        for (int i=0;i<nameList.length ;i++){
            model.add(i,nameList[i]);
        }
        targetList.setModel(model) ;
        /*targetList.setModel(new javax.swing.AbstractListModel() {

            String[] strings = {"item1","item2"};// { "background", "selection", "axis" };
            if(nameList!=)
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });*/
        listScrollPane.setViewportView(targetList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 0, 7);
        add(listScrollPane, gridBagConstraints);

        //maintSwatch.setBorder(new javax.swing.border.TitledBorder("Select..."));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        add(maintSwatch, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 6);
        add(recentSwatch, gridBagConstraints);

        selectedPane.setBorder(new javax.swing.border.TitledBorder("Select..."));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        //add(selectedPane, gridBagConstraints);

        selectL.setText("selected");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        //add(selectL, gridBagConstraints);
        //add by jin
        JButton resetBtn=new JButton("reset");
                    resetBtn.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                //System.out.println("*** repaint start:"+Calendar.getInstance().getTime().getTime()+"\n");
                                 ColorPalette.this.firePropertyChange("ColorReset",null,null);
                                //System.out.println("repaint canvas <-");
                            }
                    });
        resetBtn.setMargin(new Insets(0,10,0,10));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        //.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(resetBtn, gridBagConstraints);

    }
    private void postInit(){
        targetList.setSelectedIndex(0);
        this.recentSwatch.addMouseListener(new RecentSwatchListener());
        this.maintSwatch.addMouseListener(new MainSwatchListener());
        //this.selectedPane.addMouseListener(new MainSwatchListener());
    }

    /*******************************************************************************************************
     *                event control
     *******************************************************************************************************/
     public class RecentSwatchListener extends MouseAdapter implements Serializable {
        public void mousePressed(MouseEvent e) {
           int clickCount= e.getClickCount();
           if(clickCount==1){
	        Color color = recentSwatch.getColorForLocation(e.getX(), e.getY());
            //selectedPane.setSelectedColor(color);
               setSelectedColor(color);
	        //System.out.println("select color:"+color);
           }
           else if(clickCount==2){
               Color color=JColorChooser.showDialog(ColorPalette.this,"Please choose a color",Color.white );
               recentSwatch.setMostRecentColor(color);
               //selectedPane.setSelectedColor(c);
               setSelectedColor(color);
           }
        }

	}
    public class MainSwatchListener extends MouseAdapter implements Serializable {
        public void mousePressed(MouseEvent e) {
	    Color color = maintSwatch.getColorForLocation(e.getX(), e.getY());
        //selectedPane.setSelectedColor(color);
        setSelectedColor(color);
	    //System.out.println("click color:"+color);
        }

	}
    private void setSelectedColor(Color color){
       selectedPane.setSelectedColor(color);
       String target=(String) this.targetList.getSelectedValue() ;
       this.firePropertyChange(target,null,selectedPane.getSelectedColor());
       System.out.println(target+"'s color:"+selectedPane.getSelectedColor());
    }

  
     // Variables declaration - do not modify

    private javax.swing.JScrollPane listScrollPane;
    private javax.swing.JLabel selectL;
    private javax.swing.JList targetList;
    private SelectPanel selectedPane;
    private MainSwatchPanel maintSwatch;
    private RecentSwatchPanel recentSwatch;
}